package com.exam.dao.role;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import com.exam.common.vo.MenuVO;
import com.exam.common.vo.UserVO;
import com.exam.dao.base.BaseDaoImpl;
import com.exam.entity.Menu;
import com.exam.entity.RoleMenu;

@Repository("RoleDaoImp")
public class RoleDaoImp extends BaseDaoImpl<RoleMenu> implements RoleDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<MenuVO> getMenuVO(UserVO userVO) {
		List<RoleMenu> roleMenu = this.getSession()
				.createQuery(" from RoleMenu where roleId=(select roleId from UserRole where userId=:userId)")
				.setParameter("userId", Integer.parseInt(userVO.getUserName()))
				.list();
		List<Integer> mnIdList = new ArrayList<Integer>();
		for(RoleMenu rm:roleMenu){
			mnIdList.add(rm.getMenuId());
		}
		
		List<Menu> menu = this.getSession().createQuery("from Menu where menuId in (:roleList)")
				.setParameterList("roleList", mnIdList)
				.list();
		
		List<MenuVO> list = new ArrayList<MenuVO>();
		for(Menu mn:menu){
			MenuVO menuVO = new MenuVO();
			BeanUtils.copyProperties(mn, menuVO);
			list.add(menuVO);
		}
		return list;
	}
	
}
